// pages/addAddress/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePaths: '',
    uploadFile: '',
    status: 0,
    title:'上传体检报告'
  },
  /**
   * 打开上传图片
   */
  uploadImage(e){
    var that = this
    if(that.data.status == 0){
    wx.showLoading({
      title: '加载中',
    })
    var token = wx.getStorageSync('token');
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        wx.hideLoading()
        const tempFilePaths = res.tempFilePaths
        that.setData({
          tempFilePaths: tempFilePaths
        })
        for(var i=0; i<tempFilePaths.length; i++){
          wx.uploadFile({
            url: 'http://127.0.0.1:8080/common/upload', //上传图片
            filePath: tempFilePaths[i],
            header: {
              'Authorization': token
            },
            name: 'file',
            success (res){
              const data = JSON.parse(res.data)
              that.setData({
                uploadFile: data['url']
              })
              that.addTopic()
              console.log(that.data.uploadFile)
            },
          })
        }
      },
      fail(res){
        wx.hideLoading()
        wx.showToast({
          title: "取消上传",
          icon: 'none',
        })
      }
    })
    } else {
      var token = wx.getStorageSync('token');
      var user = wx.getStorageSync('user');
      var that = this;
      wx.request({
        url: 'http://127.0.0.1:8080/system/user/report/'+user['userId'], // 上传个人报告接口
        method: 'delete',
        header: {
          'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错
          'Authorization': token
        },
        success(res) {
          if (res.statusCode==200) {
            user['report'] = ''
            wx.setStorageSync('user', user)
            that.setData({
              uploadFile: '',
              title: '上传体检报告',
              status: 0
            })
            wx.showToast({
              title: "删除成功",
              icon: 'none',
            })
          }
        }
      })
    }
  },
  onLoad: function(){
    var that = this
    var user = wx.getStorageSync('user');
    if(user['report'] != ''){
      that.setData({
        uploadFile: user['report'],
        status: 1,
        title: "删除体检报告"
      })
    }
  },
  
  addTopic: function() {
    var token = wx.getStorageSync('token');
    var user = wx.getStorageSync('user');
    var that = this;
    wx.request({
      url: 'http://127.0.0.1:8080/system/user/report', // 上传个人报告接口
      method: 'put',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错
        'Authorization': token
      },
      data: {
        userId: user['userId'],
        report: that.data.uploadFile
      },
      success(res) {
        if (res.statusCode==200) {
          user['report'] = that.data.uploadFile
          wx.setStorageSync('user', user)
          wx.showToast({
            title: "添加成功",
            icon: 'none',
          })
          that.setData({
            uploadFile: user['report'],
            status: 1,
            title: "删除体检报告"
          })
        }
      }
    })
  }
})